package com.fhz.day009;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.work.Constraints;
import androidx.work.Data;
import androidx.work.NetworkType;
import androidx.work.OneTimeWorkRequest;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;

import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "feng";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //4约束
        Constraints constraints = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.UNMETERED)
                .setRequiresCharging(true)

                .build();

        //1,对象是work
        //uplaodworker

        //2,怎么做
        OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(UploadWorker.class)
//                .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
//                .setConstraints(constraints)
//                .setInitialDelay(10, TimeUnit.MINUTES)
//                .setBackoffCriteria(
//                        BackoffPolicy.LINEAR,
//                        OneTimeWorkRequest.MIN_BACKOFF_MILLIS,
//                        TimeUnit.MILLISECONDS)
                .addTag("cleanup")
                .setInputData(
                        new Data.Builder()
                                .putString("IMAGE_URI", "http://...")
                                .build()
                )
                .build();

        //任务 2
        OneTimeWorkRequest workRequest2 = new OneTimeWorkRequest.Builder(UploadWorker.class)
//                .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
//                .setConstraints(constraints)
//                .setInitialDelay(10, TimeUnit.MINUTES)
//                .setBackoffCriteria(
//                        BackoffPolicy.LINEAR,
//                        OneTimeWorkRequest.MIN_BACKOFF_MILLIS,
//                        TimeUnit.MILLISECONDS)
                .addTag("cleanup")
                .setInputData(
                        new Data.Builder()
                                .putString("IMAGE_URI", "http:2222/...")
                                .build()
                )
                .build();

        //重复执行
        PeriodicWorkRequest saveRequest =
                        new PeriodicWorkRequest.Builder(UploadWorker.class, 1, TimeUnit.DAYS)
                        // Constraints
                        .build();

        //3,调度
//        WorkManager.getInstance(this).enqueue(workRequest); //执行
        //多任务
        WorkManager.getInstance(this).beginWith(workRequest).then(workRequest2).enqueue();


        //观察进度
        WorkManager.getInstance(getApplicationContext())
                // requestId is the WorkRequest id
                .getWorkInfoByIdLiveData(workRequest.getId())
                .observe(this, new Observer<WorkInfo>() {
                    @Override
                    public void onChanged(@Nullable WorkInfo workInfo) {
                        if (workInfo != null) {
                            Data progress = workInfo.getProgress();
                            int value = progress.getInt("PROGRESS", 0);
                            // Do something with progress
                            Log.i(TAG, "onChanged: "+ value);
                        }
                    }
                });


    }
}